package com.iscale.controller;

import com.iscale.model.dto.Pager;
import com.iscale.model.entity.BaseBrandInfo;
import com.iscale.model.entity.BaseFileInfo;
import com.iscale.model.entity.SysBusinessType;
import com.iscale.service.BaseBrandInfoService;
import com.iscale.service.BaseFileInfoService;
import com.iscale.util.ResponseCode;
import com.iscale.util.Result;
import com.iscale.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.iscale.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 品牌管理 前端控制器
 * </p>
 *
 * @author Andychen
 * @since 2017-03-10
 */
@Api(value = "品牌管理模块",description = "品牌管理模块")
@Controller
@RequestMapping("/baseBrandInfo")
public class BaseBrandInfoController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BaseBrandInfoController.class);

    @Autowired
    BaseFileInfoService fileInfoService;

    @Autowired
    BaseBrandInfoService brandInfoService;

    @ApiOperation(value = "添加/修改品牌", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "saveOrUpdateBrand", method = RequestMethod.POST)
    public Result saveOrUpdateBrand(
            @ApiParam(value = "名称", required = true) @RequestParam(defaultValue = "", required = true) String name,
            @ApiParam(value = "商户编号", required = false) @RequestParam(defaultValue = "", required = false) String merchantNo,
            @ApiParam(value = "描述", required = false) @RequestParam(defaultValue = "", required = false) String description,
            @ApiParam(value = "状态", required = false) @RequestParam(defaultValue = "", required = false) Integer status,
            @ApiParam(value = "Logo", required = false) @RequestParam(required = false) MultipartFile file
    ) throws Exception {
        //上传文件
        BaseFileInfo fileInfo = null;
        Long fid = null;
        String fUrl = null;
        if(null!=file){
            List<BaseFileInfo> fileInfoList = fileInfoService.saveFileInfos(new MultipartFile[]{file} ,merchantNo);
            if(null!=fileInfoList && fileInfoList.size()>0){
                fileInfo = fileInfoList.get(0);
                fid = fileInfo.getId();
                fUrl = fileInfo.getFileUrl();
            }
        }
        //提交表单  String merchantNo, String name, String description, Long logoFile, String createUser, Integer status
        BaseBrandInfo brandInfo = new BaseBrandInfo(merchantNo,name,description,fid,"",status,fUrl);


        boolean result =  brandInfoService.insertOrUpdate(brandInfo);
        if(result){
            return Result.success(brandInfo);
        }else{
            return Result.error(ResponseCode.add_brand_error);
        }
    }

    @ApiOperation(value = "添加/修改品牌2", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "insertBrand", method = RequestMethod.POST)
    public Result insertBrand(
            @ApiParam(value = "名称", required = true) @RequestParam(defaultValue = "", required = true) String name,
            @ApiParam(value = "ID", required = false) @RequestParam(defaultValue = "", required = false) Long id,
            @ApiParam(value = "商户编号", required = false) @RequestParam(defaultValue = "", required = false) String merchantNo,
            @ApiParam(value = "描述", required = false) @RequestParam(defaultValue = "", required = false) String description,
            @ApiParam(value = "状态", required = false) @RequestParam(defaultValue = "", required = false) Integer status,
            @ApiParam(value = "文件ID", required = false) @RequestParam(required = false) Long fid,
            @ApiParam(value = "文件Url", required = false) @RequestParam(required = false) String fUrl
    ) throws Exception {
        BaseBrandInfo brandInfo = new BaseBrandInfo(id,merchantNo,name,description,fid,"",status,fUrl);

        boolean result =  brandInfoService.insertOrUpdate(brandInfo);
        if(result){
            return Result.success(brandInfo);
        }else{
            return Result.error(ResponseCode.add_brand_error);
        }
    }

    @ApiOperation(value = "分页查找品牌", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "selectBrandsByPager", method = RequestMethod.POST)
    public Result selectBrandsByPager(
            @ApiParam(value = "当前页", required = true) @RequestParam(defaultValue = "1", required = true) int page,
            @ApiParam(value = "每页显示数", required = true) @RequestParam(defaultValue = "10", required = true) int size,
            @ApiParam(value = "商户编号", required = false) @RequestParam(defaultValue = "", required = false) String merchantNo
    ) throws Exception {

        //提交表单  String merchantNo, String name, String description, Long logoFile, String createUser, Integer status
        Pager<BaseBrandInfo> pager = brandInfoService.selectBrandsByPager(page,size,merchantNo);

        return Result.success(pager);
    }

    @ApiOperation(value = "ID查找品牌", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "selectBrandsById", method = RequestMethod.POST)
    public Result selectBrandsById(
            @ApiParam(value = "ID", required = true) @RequestParam(defaultValue = "1", required = true) Long id
    ) throws Exception {

        //提交表单  String merchantNo, String name, String description, Long logoFile, String createUser, Integer status
        BaseBrandInfo pager = brandInfoService.selectById(id);

        return Result.success(pager);
    }
}
